package interfaze;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import logic.Utilities;

@SuppressWarnings("serial")
public class BrickBreakerInstructionsPanel extends JPanel implements ActionListener, MouseListener {
	
	private SuperArcade2in1 window;								//The JFrame where this panel is being added.
	private int counter = 0;									//The index of the list of the instruction.
	//The list of instruction images.
	private String[] instructionName = {"instructions_0.png", "instructions_3.png", "instructions_4.png", "instructions_1.png", 
				"instructions_2.png", "instructions_5.png", "instructions_6.png", "instructions_7.png", "instructions_8.png", 
				"instructions_9.png", "instructions_10.png", "instructions_bug.png"};
	private JLabel instructionLabel = new JLabel();				//The image displayed in the middle of the Panel.
	private JLabel messageLabel = new JLabel("", JLabel.CENTER);	//The label that contains the instructions.
	private JLabel background = new JLabel();					//The Panel's background.
	private JLabel helpLabel = new JLabel("", JLabel.CENTER);	//The label related to the back button.
	private JButton bPrevious = new JButton("Previous");		//The Previous Button.
	private JButton bNext = new JButton("Next");				//The Next Button.
	private JButton bBack = new JButton("Back");				//The Back Button.
	private Font font;											//The font for all the text labels and buttons.
	
	public BrickBreakerInstructionsPanel(SuperArcade2in1 window) {
		this.window = window;
		//Sets the panel's layout to null in order to work with absolute coordinates.
		this.setLayout(null);
		
		//Sets the font for all the text labels and buttons.
		font = new Font("Helvetica", Font.PLAIN, Math.round(12 * window.xScale));
		
		//Sets background
		Image image = logic.Utilities.loadImage("res/background.png");
		if(window.xScale != 1 || window.yScale != 1) {
			image = Utilities.resizeImage(window.xScale, window.yScale, "res/background.png");
		}
		background.setIcon(new ImageIcon(image));
		background.setBounds(0, 0, image.getWidth(null), image.getHeight(null));
		
		
		//Sets all the components into the panel.
		bBack.setBounds(Math.round(25 * window.xScale), Math.round(625 * window.yScale), 
				Math.round(100 * window.xScale), Math.round(25 * window.yScale));
		bBack.setFont(font);
		bPrevious.setEnabled(false);
		bPrevious.setBounds(Math.round(100 * window.xScale) , Math.round((window.height / 2 ) - (25 * window.yScale / 2)),
				Math.round(100 * window.xScale), Math.round(25 * window.yScale));
		bPrevious.setFont(font);
		bNext.setBounds(Math.round(window.width - (200 * window.xScale)), Math.round((window.height / 2 ) - (25 * window.yScale / 2)),
				Math.round(100 * window.xScale), Math.round(25 * window.yScale));
		bNext.setFont(font);
		
		helpLabel.setBounds(Math.round(window.width / 2 - (130 * window.xScale / 2)), Math.round(625 * window.yScale), 
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		helpLabel.setForeground(Color.WHITE);
		helpLabel.setFont(font);
		
		//Selects the first image of the instructions.
		instructionImageSelect(counter);
		
		messageLabel.setFont(font);
		messageLabel.setForeground(Color.WHITE);
		messageLabel.setBounds(Math.round(225 * window.xScale), Math.round(470 * window.yScale), 
				Math.round(830 * window.xScale), Math.round(150 * window.yScale));
		
		//Sets the instruction's first message.
		instructionMessageSelect(counter);
		
		//Adds all the components to the panel.
		this.add(messageLabel);
		this.add(instructionLabel);
		this.add(bPrevious);
		this.add(bNext);
		this.add(bBack);
		this.add(helpLabel);
		this.add(background);
		
		//Adds all the buttons listeners.
		bBack.addActionListener(this);
		bPrevious.addActionListener(this);
		bNext.addActionListener(this);
		bBack.addMouseListener(this);
		bPrevious.addMouseListener(this);
		bNext.addMouseListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(bBack)) {
			//If the back button is pressed redirects to the main menu.
			window.add(new BrickBreakerMainMenuPanel(window));
			this.setVisible(false);
			window.remove(this);
		} if(e.getSource().equals(bPrevious)) {
			//shows the previous image and text message if it's not the first one already.
			if(counter > 0) {
				counter--;
			}
			if(counter < instructionName.length - 1) {
				bNext.setEnabled(true);
			}
			if(counter == 0) {
				bPrevious.setEnabled(false);
			}
			//Updates the image and the instruction's message.
			instructionImageSelect(counter);
			instructionMessageSelect(counter);
		} else if(e.getSource().equals(bNext)) {
			//shows the next image and text message if it's not the last one already. 
			if(counter < instructionName.length - 1) {
				counter++;
			}
			if(counter == instructionName.length - 1) {
				bNext.setEnabled(false);
			}
			if(counter > 0) {
				bPrevious.setEnabled(true);
			}
			//Updates the image and the instruction's message.
			instructionImageSelect(counter);
			instructionMessageSelect(counter);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		//Increases the buttons size when the mouse enters them.
		if(e.getSource().equals(bBack)) {
			Utilities.buttonEffect(bBack, 5, window.xScale, window.yScale);
			helpLabel.setText("Returns to Main Menu");
		} else if(e.getSource().equals(bPrevious)) {
			Utilities.buttonEffect(bPrevious, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bNext)) {
			Utilities.buttonEffect(bNext, 5, window.xScale, window.yScale);
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		//Decreases the buttons size when the mouse enters them.
		if(e.getSource().equals(bBack)) {
			Utilities.buttonEffect(bBack, -5, window.xScale, window.yScale);
			helpLabel.setText("");
		} else if(e.getSource().equals(bPrevious)) {
			Utilities.buttonEffect(bPrevious, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bNext)) {
			Utilities.buttonEffect(bNext, -5, window.xScale, window.yScale);
		}
	}
	
	/** Changes the image shown at the middle of the panel.
	 * @param index : the number image located in the imageName array that wants to be shown from zero to eleven.
	 */
	public void instructionImageSelect(int index) {
		//Setting Instruction Image
		Image image = Utilities.loadImage("res/" + instructionName[counter]);
		ImageIcon icon = new ImageIcon(image);
		image = Utilities.resizeImage(window.xScale, window.yScale, "res/" + instructionName[index]);
		icon = new ImageIcon(image);
		instructionLabel.setIcon(icon);
		instructionLabel.setBounds(Math.round(225 * window.xScale),Math.round((window.height / 2) - ((icon.getIconHeight() / 2) + 
				Math.round(100 * window.yScale))), Math.round(830 * window.xScale), icon.getIconHeight());
	}
	
	/** Changes the message shown under the image.
	 * @param index : the number of the instruction that wants to be shown from zero to eleven.
	 */
	public void instructionMessageSelect(int index) {
		switch(index) {
		case 0:
			messageLabel.setText("Welcome to the Brick Breaker's instruction menu, please click the Next button to begin the explanation.");
			break;
		case 1:
			messageLabel.setText("Press and hold D key on your keyboard in order to move the bat to the right.");
			break;
		case 2:
			messageLabel.setText("Press and hold A key on your keyboard in order to move the bat to the left.");
			break;
		case 3:
			messageLabel.setText("<html><center>Whenever you see that the ball touches and follows the bat's moves, press Space key on your" +
					" keyboard <br> in order to launch the ball to the right.</html>");
			break;
		case 4:
			messageLabel.setText("<html><center>You can also try combining Space plus A or D keys on your keyboard in order to launch the ball" +
					" with <br> the same direction of the bat's movement. <br> You will notice that if you don't combine those keys " + 
					"the ball will be launched to the right as default.<html>");
			break;
		case 5:
			messageLabel.setText("<html><center>Sometimes a bonus will appear when you break a brick. There are different kind of bonuses," +
					" each one with a unique effect. <br> In this case a Shooter bonus appeared.</html>");
			break;
		case 6:
			messageLabel.setText("If you catch them, a number will appear under the bat adding five units each time you touch it.");
			break;
		case 7:
			messageLabel.setText("<html><center>You can now press the S key on your keyboard in order to launch a Missile, using them " +
					"will subtract one unit to the counter.");
			break;
		case 8:
			messageLabel.setText("<html><center>You won't receive any bonus when a missile breaks a brick. <br>" + 
			" Each of those bricks will score half of the points you would obtained if you had broken them with the ball. <br>" +
			" When the counter reaches zero, you won't be able to launch more missiles until you get another shooter bonus.<html>");
			break;
		case 9: 
			messageLabel.setText("<html><center>This is a double ball bonus, if you achieve to catch them they'll duplicate the amount of" +
					" balls inside the playground until certain numbers of balls is reached.");
			break;
		case 10:
			messageLabel.setText("Your life counter won't decreace until you lose all the balls. Keep that in mind when you see another double" +
					" ball bonus.");
			break;
		case 11:
			messageLabel.setText("<html><center>That's all for newbies. Keep on playing and discovering all secrets and techniques. <br>" +
					" (this may not happen)<html>");
			break;
		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}
}
